﻿/**
  KCAKCAK   CAKCAKC   AK      CA
 AK        KC     CA  AK    AK
CA         KC     CA  AK  KC
CA         KCAKCAKCA  AKCA
CA         KC     CA  AK  KC
 AK        KC     CA  AK    AK
  KCAKCAK  KC     CA  AK      CA

_________________________________________________________________
 China ataca KamGameka - copyright 2007(c)Ruben Sainz de la Maza
 
	@class: 	Game
	@vertion: 	0.1
	@date:		25/02/08
				
	@language: 	ActionScript 3.0
	@author:	Ruben Sainz de la Maza
			
**/

try { var dummy = Game; } catch(e)
{

	/**
	 * constructor
	 */
	function Game()
	{
		trace("#Game# constructor");
	}




//	Manager Users

	/**
	 * pushUserLobby
	 */
	Game.prototype.pushUserLobby = function(p_null, p_strNick)
	{
		if(application.objUsers[p_strNick]){
			application.objUsers[p_strNick].room = "lobby";
			application.objRooms["lobby"][p_strNick] = application.objUsers[p_strNick];
		//	this.getUsersPerRoom();
		
			trace("#Game# Lobby:" + application.objUsers[p_strNick].room + " IN: " + p_strNick);
			return true;
		} else return false;
	}
	

	/**
	 * popUserLobby
	 */
	Game.prototype.popUserLobby = function(p_null, p_strNick)
	{
		if(application.objRooms["lobby"][p_strNick]){
			application.objUsers[p_strNick].room = null;
			application.objRooms["lobby"][p_strNick] = null;
		
			trace("#Game# popUserLobby:" + p_strNick + " OUT lobby");
			return true;
		} else return false;
	}
	

	/**
	 * pushUserRoom
	 */
	Game.prototype.pushUserRoom = function(p_null, p_strNick, p_strRoom)
	{
		if(application.objRooms[p_strRoom]){														//verificar que existe la sala
			if(application.objUsers[p_strNick]){													//verificar que existe el usuario
				if(application.objRooms["lobby"][p_strNick]) this.popUserLobby(null, p_strNick); 	//si esta en el lobby quitarlo
				
				//ingresarlo al room
				application.objUsers[p_strNick].room = p_strRoom;
				application.objRooms[p_strRoom][p_strNick] = application.objUsers[p_strNick];
			}			
			trace("#Game# pushUserRoom:" + p_strNick + " IN room: " + p_strRoom);
			return true;
			
		} else {
			trace("#Game# pushUserRoom: la sala " + p_strRoom + " no existe");
			return false;
		}
	}
	
	
	/**
	 * popUserRoom
	 */
	Game.prototype.popUserRoom = function(p_null, p_strNick)
	{
		if(application.objUsers[p_strNick]){													//verificar que existe el usuario
			if(application.objUsers[p_strNick].room != "lobby"){								//verificar que no esta en el lobby
				application.objRooms[application.objUsers[p_strNick].room][p_strNick] = null; 	//eliminarlo de la sala
				trace("#Game# popUserRoom:" + p_strNick + " pasa al lobby");
				this.pushUserLobby(null, p_strNick);											//ingresarlo al lobby				
				
			} else {
				trace("#Game# popUserRoom:" + p_strNick + " ya se encuentra en el lobby");
				return false;
			}
			
		} else {
			trace("#Game# popUserRoom:" + p_strNick + " no existe");
			return false;
		}
	}
	

	/**
	 * setColor
	 */
/*	Game.prototype.setColor = function(p_null, p_strNick, p_strColor)
	{
		if(application.objUsers[p_strNick]){								//verificar si existe el usuario
		
			var roomActual = application.objUsers[p_strNick].room;

			for(var i in application.objUsers){
				if(application.objUsers[i].room == roomActual)
					application.objUsers[i].call("Game/changeColor", false, p_strNick, p_strColor);
			}			
			
			trace("#Game# setColor:" + p_strNick + " Color: " + p_strColor);
		}
	}
	*/
	
	/**
	 * getUsersPerRoom
	 * 
	 * @usage   envia un array con los numeros de usuarios conectados a cada sala a todos los usuarios
	 */
/*	Game.prototype.getUsersPerRoom = function()
	{
		var arr  = new Array(0, 0, 0, 0, 0);
		var obj  = new Object();
		obj.arr0 = new Array();		//lobby
		obj.arr1 = new Array();		//gym
		obj.arr2 = new Array();		//basket
		obj.arr3 = new Array();		//bar
		obj.arr4 = new Array();		//baseball
				
		for(var i in application.objUsers){
			
			if(application.objUsers[i] != null){
				var n = Number(application.objUsers[i].intRoom);
				arr[n]++;
				obj["arr" + n].push(application.objUsers[i]);
			}
		}
		
		//reenviar a todos los conectados
		for(var i in application.objUsers){
			var arrNicks = obj["arr"+application.objUsers[i].intRoom];
		//	for(var n in arrNicks)for(var m in arrNicks[n])trace(m+" "+arrNicks[n][m])
			if(application.objUsers[i] != null)
				application.objUsers[i].call("Game/setUsersPerRoom", false, arr, arrNicks);
		}
		
	}
*/	

	/**
	 * mover a un usuario de una sala a otra
	 * @usage   
	 * @param   p_null    
	 * @param   p_strNick nombre de usuario
	 * @param   p_intRoom numero de sala
	 * @param   p_intX    posicion en x
	 * @param   p_intY    posicion en y
	 */
/*	Game.prototype.moveUserInRoom = function(p_null, p_strNick, p_intRoom, p_intX, p_intY)
	{
	//	trace("mover");
		var user = application.objUsers[p_strNick];
		if(user){
			user.x = p_intX;
			user.y = p_intY;
			for(var i in application.objUsers){
				if(application.objUsers[i].intRoom == p_intRoom)
					application.objUsers[i].call("Game/moveUser", false, p_strNick, p_intX, p_intY);
			}
		}
	}
*/	
	

}